package cn.zjj._02_xhr_2;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

import java.nio.charset.StandardCharsets;

/**
 * 生产者
 */
public class Producer {
    public static void main(String[] args) throws Exception {
        //1.生产者对象
        DefaultMQProducer producer = new DefaultMQProducer("xhr2_producer_producer");
        //2.设置nameserver
        producer.setNamesrvAddr("localhost:9876");
        //3.启动
        producer.start();
        //4.创建并发送消息
        Message message = new Message("user","login","这是user标题下的login消息传递".getBytes(StandardCharsets.UTF_8));
        producer.send(message, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.println("成功后的回调信息!"+sendResult);
            }

            @Override
            public void onException(Throwable throwable) {
                throwable.printStackTrace();
                System.out.println("失败后的回调");
            }
        });
        System.out.println("此句先执行代表异步成功");
       /* //5.关闭  异步中关闭会导致回调不成功
        producer.shutdown();*/

    }

}
